package com.seventeen.mq.proxy.server.controller;

import com.alibaba.fastjson.JSON;
import com.seventeen.mq.proxy.common.core.domain.AjaxResult;
import com.seventeen.mq.proxy.server.proxy.ext.ExtSendMessage;
import com.seventeen.mq.proxy.server.proxy.producer.ProducerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 代理消息发送Controller
 *
 * @author seventeen
 */
@RestController
@RequestMapping(value = "/mq/proxy/sendMessage")
public class SendMessageController
{

    public static final Logger LOGGER = LoggerFactory.getLogger(SendMessageController.class);

    @Autowired
    private ProducerFactory producerFactory;
    @PostMapping(value = "/sendSync")
    public AjaxResult sendSync(@Validated @RequestBody ExtSendMessage sendMessage)
    {
        try
        {
            producerFactory.sendSync(sendMessage);
            return AjaxResult.success("消息发送成功");
        }
        catch (Exception e)
        {
            LOGGER.error("send message fail, sendMessage {}, error {}", JSON.toJSONString(sendMessage), e.getMessage(), e);
            return AjaxResult.error("消息发送失败");
        }
    }
}
